const service = require('../../utils/service.js')
const api = require('../../utils/util.js')
Page({
  data: {
    type: 1,
    shopData: {},
    userInfo: {},
    shopInfo: {},
    page: 1,
    goodsList: [],
    hasMoreData: true,
    choicArr: [],
    classfiyList: [],
    showClassfiy: false,
    showMyModel: false,
    classfiyName: '',
    choicClassfiy: ''
  },

  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    this.setData({
      userInfo: userInfo
    })
    if (options.type) {
      let type = options.type
      this.setData({
        type: type
      })
    }
    
    this.init()
  },

  init: function () {
    this.getShopInfo(() => {
      this.getGoodsList()
      this.getClassfiyList()
    })
  },

  // 获取店铺信息
  getShopInfo: function (cb) {
    let that = this;
    let shopData = wx.getStorageSync('shop_data_shop');
    api.ajax(service.url('getShopDetail'), {
      userid: wx.getStorageSync('out_user_id')
    }, function (res) {
      that.setData({
        shopData: shopData,
        shopInfo: res.data
      })
      cb()
    })
  },

  // 获取商品列表
  getGoodsList: function () {
    let that = this
    api.ajax(service.url('productListGet'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: that.data.shopInfo.shop_code,
      on_sale: that.data.type,
      page: that.data.page,
    }, (res) => {
      if (res.code == 0) {
        if (res.data.length > 0) {
          res.data.forEach((item, index) => {
            this.data.goodsList.push(item)
          })
          this.setData({
            goodsList: this.data.goodsList,
            hasMoreData: true
          })
        } else {
          this.setData({
            hasMoreData: false
          })
        }
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取分类
  getClassfiyList: function () {
    let that = this
    api.ajax(service.url('categoryGetAll'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: this.data.shopInfo.shop_code,
    }, function (res) {
      if (res.code == 0) {
        that.setData({
          classfiyList: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取复选框的值
  checkboxChange: function (e) {
    let val = e.detail.value
    this.setData({
      choicArr: val
    })
  },

  // 上、下架商品
  lowerGoods: function () {
    let that = this
    let option = this.data.choicArr
    if (option.length <= 0) {
      api.toast('请先选择商品')
      return
    }
    let goods_id = ''
    for (var i = 0, len = option.length; i < len; i++) {
      goods_id += option[i] + ','
    }
    let toastMsg = ''
    let type = 0
    if (this.data.type == 2) {
      toastMsg = '确定上架这些商品?'
      type = 1
    } else {
      toastMsg = '确定下架这些商品?'
      type = 0
    }

    wx.showModal({
      title: '提示',
      content: toastMsg,
      success: (rel) => {
        if (rel.confirm) {
          api.ajax(service.url('productGoodsOnSale'), {
            unionid: that.data.shopData.shop_unionid,
            token: that.data.shopData.token,
            token_key: that.data.shopData.token_key,
            shop_code: that.data.shopInfo.shop_code,
            goods_id: goods_id,
            on_sale: type
          }, function (res) {
            if (res.code == 0) {
              api.toast('操作成功')
              that.setData({
                page: 1,
                goodsList: []
              })
              that.getGoodsList()
            } else {
              api.toast(res.message)
            }
          })
        }
      }
    })
  },

  // 分类至
  sortTo: function () {
    let option = this.data.choicArr
    if (option.length <= 0) {
      api.toast('请先选择商品')
    } else {
      this.showClassfiy()
    }
  },
  // 确定分类至
  sureSotrTo: function () {
    let that = this
    let choicGoods = ''
    let choicArr = this.data.choicArr
    for (var i = 0, len = choicArr.length; i < len; i++) {
      choicGoods += choicArr[i] + ','
    }
    let choicClassfiy = this.data.choicClassfiy
    if (!choicClassfiy) {
      api.toast('请选择分类')
      return
    }

    api.ajax(service.url('productGoodsChangeCategory'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: this.data.shopInfo.shop_code,
      category_id: choicClassfiy,
      goods_id: choicGoods
    }, function (res) {
      if (res.code == 0) {
        api.toast('操作成功')
        that.hideClassfiy()
      } else {
        api.toast(res.message)
      }
    })
  },

  // 确定新建分类
  sureAddClassfiy: function () {
    let that = this
    let val = this.data.classfiyName
    if (val == '') {
      api.toast('请输入分类名称')
      return
    }
    api.ajax(service.url('categoryAdd'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      name: val,
      pid: 0,
      shop_code: this.data.shopInfo.shop_code
    }, (res) => {
      if (res.code == 0) {
        this.setData({
          showMyModel: false
        })
        api.toast('新建分类成功')
        this.getClassfiyList()
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取分类名称
  getClassfiyName: function (e) {
    let val = e.detail.value
    this.setData({
      classfiyName: val
    })
  },

  // 选择分类
  radioChange: function (e) {
    let classfiy_id = e.detail.value
    this.setData({
      choicClassfiy: classfiy_id
    })
  },

  // 显示分类
  showClassfiy: function () {
    this.setData({
      showClassfiy: true,
      choicClassfiy: ''
    })
  },
  // 隐藏分类
  hideClassfiy: function () {
    this.setData({
      showClassfiy: false
    })
  },


  // 显示带输入框弹层
  hideMyModel: function () {
    this.setData({
      showMyModel: false
    })
  },
  // 隐藏带输入框弹层
  showMyModel: function () {
    this.setData({
      showMyModel: true
    })
  },

  // 禁止滑动
  myCatchTouch: function () {
    return
  },

  onReachBottom: function () {
    if (this.data.hasMoreData) {
      let page = this.data.page += 1
      this.setData({
        page: page
      })
      this.getGoodsList()
    } else {
      api.toast('没有更多数据了')
    }
  }
})